﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data;

namespace DAL
{
    public class AccountDAL:Base<Account>
    {
        public AccountDAL()
            : base("account", "AID")
        {
        }



        public override bool Insert(Account entity)
        {
            string sql = "insert into account (email,password,account) values(@email,@password,@account)";
            var f = MySQLHelper.ExecuteNonQuery(sql
                , new KeyValuePair<string, object>("email", entity.Email)
                , new KeyValuePair<string, object>("password", entity.Password)
                , new KeyValuePair<string, object>("account", entity.AccountName)) == 1;
            if (f)
            {
                //插入成功
                entity.ID = GetLastInsertID();
            }
            return f;
            
        }

        public DataTable GetEntityByEmailAndPassword(string email,string password)
        {
            string sql = "select * from account where email=@email and password = @password";
            var table = MySQLHelper.Query(sql, new KeyValuePair<string, object>("email", email)
                , new KeyValuePair<string, object>("password", password));
            return table;
        }

        public override bool Update(Account entity)
        {
            throw new NotImplementedException();
        }

        public override Account GetEntityById(int id)
        {
            string sql = "select * from account where AID=" + id;
            var table = MySQLHelper.Query(sql);
            if (table.Rows.Count==1)
            {
                Account account = new Account();
                account.ID = id;
                account.Password = table.Rows[0]["password"].ToString();
                account.Email = table.Rows[0]["email"].ToString();
                account.AccountName = table.Rows[0]["account"].ToString();
                return account;
            }
            else
            {
                return null;
            }
        }

        public override Account GetEntityById(string id)
        {
            int i = 0;
            var f= int.TryParse(id, out i);
            if (f)
            {
                return GetEntityById(i);
            }
            else
            {
                throw new ArgumentException();
            }
        }
    }
}
